package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.insurance;

import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.AcceptRequestDataModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.CustInfoModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 全富保规则校验
 */
public class Lbt036InsuranceStrategy implements InsuranceStrategy {

    @Override
    public InsuredModel checkInsuredRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository, ProductModel productModel, ProductConfig productConfig) {
        //被保人信息
        CustInfoModel custInfoModel = acceptRequestDataModel.getRequest().getCustInfo();
        List<InsurantModel> insurantModelList = custInfoModel.getInsurants().getInsurant();

        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(productModel.getProdInsSeq(), insurantModelList);
        //被保人职业类别：1-6
        String type = insurantModel.getOccupationType();
        //投保人与被保人的关系：本人、配偶、父母和子女
        String relation = insurantModel.getRelToApp();

        if (!(productConfig.getRelationList().contains(relation))) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    UnderWriteConstant.UNDER_WRITE_NUGG0007_MESSAGE);
        }
        if (!(AcceptConstant.OCCUPATION_ONE_TO_SIX.contains(type))) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_NPGG0028_MESSAGE, type));
        }
        //既往重疾险理赔记录
        if (StringUtils.equals(AcceptConstant.CREDENTIALS_IDNO, insurantModel.getIdType())) {
            if (insuredCoreRepository.historicalMajorDiseases(insurantModel.getIdNo(), AcceptConstant.CREDENTIALS_IDNO) > 0) {
                return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                        String.format(UnderWriteConstant.UNDER_WRITE_NUGG0060_MESSAGE, insurantModel.getName()));
            }
        }
       /* if (StringUtils.isNotBlank(insurantModel.getInsuredNo()) && insuredCoreRepository.hasHistoricalMajorDiseases(insurantModel.getInsuredNo()) > 0) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_NUGG0060_MESSAGE, insurantModel.getName()));
        }*/

        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE,
                AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }
}
